package org.edu.modules.oa.controller;
import groovy.util.logging.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.oa.entity.VeOaCarBaoXianEntity;
import org.edu.modules.oa.entity.VeOaMeetEntity;
import org.edu.modules.oa.entity.VeOaScheduleEntity;
import org.edu.modules.oa.service.IVeOaScheduleService;
import org.edu.modules.oa.vo.VeOaCarBaoXianExcelVo;
import org.edu.modules.oa.vo.VeOaScheduleExcleVo;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Api(tags="我的日程安排")
@RestController
@RequestMapping("oa/calendarList")
@Slf4j
public class VeOaScheduleController {

    @Resource
    private IVeOaScheduleService iVeOaScheduleService;

    // 日志注解
    @AutoLog(value = "日程安排-我的日程表格")
    @ApiOperation(value="我的日程表格", notes="日程安排-我的日程表格")
    // 显示表格
    @PostMapping(value = "/showTable")
    public Result<?> showTable(Integer startPage, Integer pageSize, VeOaScheduleEntity VeOaScheduleEntity ){
        HashMap<String, Object> map =  iVeOaScheduleService.showMySchedule(startPage,pageSize,VeOaScheduleEntity);
        return Result.OK(map);
    }

    // 日志注解
    @AutoLog(value = "日程安排-删除一条日程")
    @ApiOperation(value="删除一条日程", notes="日程安排-删除一条日程")
    // 显示表格
    @PostMapping(value = "/deleteOneCalendar")
    public Result<?> deleteOneSchedule(Integer id){
        Integer num  =  iVeOaScheduleService.deleteOneSchedule(id);
        return Result.OK();
    }

    // 日志注解
    @AutoLog(value = "日程安排-删除多条日程")
    @ApiOperation(value="删除多条日程", notes="日程安排-删除多条日程")
    @PostMapping(value = "/deleteMoreCalendar")
    public Result<?> deleteMoreSchedule(String idStr){
        ArrayList<Integer> list=new ArrayList<>();
        String[] ids=idStr.split(",");
        for (int i = 1; i < ids.length; i++) {
            Integer id=Integer.parseInt(ids[i]);
            list.add(id);
        }
        Integer num  =  iVeOaScheduleService.deleteMoreSchedule(list);
        return Result.OK();
    }

    // 日志注解
    @AutoLog(value = "添加日程-事务类型集合")
    @ApiOperation(value="事务类型集合", notes="添加日程-事务类型集合")
    @PostMapping(value = "/loadType")
    public Result<?> selectTitleTypeList(){
        List<VeOaScheduleEntity> list=iVeOaScheduleService.showTitleTypeList();
        return Result.OK(list);
    }



    // 日志注解
    @AutoLog(value = "日程安排-添加日程")
    @ApiOperation(value="添加日程", notes="日程安排-添加日程")
    @PostMapping(value = "/insertSchedule")
    public Result<?> selectTitleTypeList(VeOaScheduleEntity veOaScheduleEntity,String start,String end,String remind){
        Integer num  =  iVeOaScheduleService.insertSchedule(veOaScheduleEntity,start,end,remind);
        return Result.OK();
    }

    // 日志注解
    @AutoLog(value = "日程安排-修改状态")
    @ApiOperation(value="修改状态", notes="日程安排-修改状态")
    @PostMapping(value = "/updateState")
    public Result<?> updateState(VeOaScheduleEntity veOaScheduleEntity){
        Integer num  =  iVeOaScheduleService.updateState(veOaScheduleEntity);
        return Result.OK();
    }

    // 日志注解
    @AutoLog(value = "日程安排-修改信息")
    @ApiOperation(value="修改信息", notes="日程安排-修改信息")
    @PostMapping(value = "/updateInfo")
    public Result<?> updateInfo(VeOaScheduleEntity veOaScheduleEntity,String start,String end,String remind){

        Integer num  =  iVeOaScheduleService.updateInfo(veOaScheduleEntity,start,end,remind);
        return Result.OK();
    }



    //日程安排 表格导出功能
    @GetMapping(value = "exportInsure")
    public ModelAndView exportInsure(){

        // Step.4 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());

        //获取需要导出的Excel数据
        List<VeOaScheduleExcleVo> list = iVeOaScheduleService.selectAllMySchedule();
        for (int i=0;i<list.size();i++){
            if(list.get(i).getContent().length()>100000){
                list.get(i).setContent("【内容过长】");
            }
        }

        //文件的文件名
        mv.addObject(NormalExcelConstants.FILE_NAME, "日程安排");
        //数据的类型
        mv.addObject(NormalExcelConstants.CLASS, VeOaScheduleExcleVo.class);

        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        ExportParams exportParams = new ExportParams("日程安排", "导出人:"+user.getRealname(), "导出信息");

        mv.addObject(NormalExcelConstants.PARAMS, exportParams);

        mv.addObject(NormalExcelConstants.DATA_LIST, list);

        return mv;
    }
    //导出模板
    @GetMapping(value = "exportList")
    public ModelAndView exportList(){

        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        List<VeOaScheduleExcleVo> excelVoList = new ArrayList<>();
        mv.addObject(NormalExcelConstants.FILE_NAME, "保险记录");
        mv.addObject(NormalExcelConstants.CLASS, VeOaScheduleExcleVo.class);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        ExportParams exportParams = new ExportParams("保险记录", "导出人:"+user.getRealname(), "导出信息");
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, excelVoList);
        return mv;
    }

    //获取日历模式下的当前月的数据集合
    // 日志注解
    @AutoLog(value = "日历模式-获取本月数据")
    @ApiOperation(value="获取本月数据", notes="日历模式-获取本月数据")
    // 显示表格
    @PostMapping(value = "/DsplayData")
    public Result<?> selectDsplayData(VeOaScheduleEntity VeOaScheduleEntity,String start,String end){
        HashMap<String, Object> map =  iVeOaScheduleService.selectDsplayData(VeOaScheduleEntity, start, end);
        return Result.OK(map);
    }
}
